package com.uhaou.common.util;

import java.util.Hashtable;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.org.rapid_framework.util.holder.ApplicationContextHolder;

import com.uhaou.system.model.SysDict;
import com.uhaou.system.model.SysUser;
import com.uhaou.system.service.ISysDictManager;
import com.uhaou.system.service.ISysUserManager;
 
public class StaticMap {

	static public Hashtable SYS_USER = null; 
	static public Hashtable SYS_DICT = null;

	/**
	 * 系统初始化，基础信息放入缓存中
	 */
	public static void initInfor(ApplicationContext ctx) {
		/*
		 * getSysNetInf(); // 系统类别网元类型 getRoomInf(); // 机房信息 getVendorInf(); //
		 * 厂商信息 getAddonsInf(); // 附加表信息 getAddonsRank();
		 */
		getUser(ctx);
		//getDept(ctx);
		getDict(ctx);
	}

	public static void getUser(ApplicationContext ctx) {
		/*
		 * ITawwpAddonsTableDao tawwpAddonsTableDao = (ITawwpAddonsTableDao)
		 * ApplicationContextHolder
		 * .getBean("tawwpAddonsTableDao"); List list = null;
		 */
		ISysUserManager mgr = (ISysUserManager) ctx.getBean("sysUserManager");
		List<SysUser> list = mgr.findAll();

		Hashtable hashtable = new Hashtable();
		for (int j = 0; j < list.size(); j++) {
			SysUser sysUser = (SysUser) list.get(j);
			hashtable.put(sysUser.getId(), sysUser.getUserName());
		}
		SYS_USER = hashtable;
	}

	/*public static void getDept() {
		ISysDeptManager mgr = (ISysDeptManager) ApplicationContextHolder
				.getBean("ItawSystemDeptManager");
		List list = mgr.geDeptdelInfos("0");

		Hashtable hashtable = new Hashtable();
		for (int j = 0; j < list.size(); j++) {
			SysDept tawSystemDept = (SysDept) list.get(j);
			hashtable.put(tawSystemDept.getId(), tawSystemDept.getDeptName());
		}
		SYS_DEPT = hashtable;
	}*/

	public static void getDict(ApplicationContext ctx) {
		ISysDictManager mgr = (ISysDictManager) ctx.
				getBean("sysDictManager");
		SysDict tawSystemDict = new SysDict();
		List<SysDict> list = mgr.findAll();
		Hashtable hashtable = new Hashtable();
		if(list!=null)
		for (int j = 0; j < list.size(); j++) {
			SysDict sysDict = (SysDict) list.get(j);
			hashtable.put(sysDict.getId(), sysDict.getDictName());

		}
		SYS_DICT = hashtable;
	}

	public static String getDictValue(String key) {
		if (key != null && !key.equals("")) {
			String value = "";
			if (SYS_DICT.get(key) != null) {
				value = SYS_DICT.get(key).toString();
			} else {
				ISysDictManager mgr = (ISysDictManager) ApplicationContextHolder.getApplicationContext()
						.getBean("ItawSystemDictManager");
				SysDict sysDict = (SysDict) mgr.getById(key);
				value = sysDict.getDictName();
				SYS_DICT.put(key, value);
			}
			return value;
		} else {
			return "";
		}
	}

	/*public static String getDeptValue(String key) {
		if (key != null && !key.equals("")) {
			String value = "";
			if (SYS_DEPT.get(key) != null) {
				value = SYS_DEPT.get(key).toString();
			} else {
				ISysDeptManager mgr = (ISysDeptManager) ApplicationContextHolder
						.getBean("ItawSystemDeptManager");
				SysDept tawSystemDept = mgr.getDeptinfobydeptid(key, "0");
				value = StaticMethod.null2String(tawSystemDept.getDeptName());
				SYS_DEPT.put(key, value);
			}
			return value;
		} else {
			return "";
		}
	}*/

	public static String getUserValue(String key) {
		if (key != null && !key.equals("")) {
			String value = "";
			if (SYS_USER.get(key) != null) {
				value = SYS_USER.get(key).toString();
			} else {
				ISysUserManager mgr = (ISysUserManager) ApplicationContextHolder.getApplicationContext()
				.getBean("sysUserManager");
				SysUser sysUser=mgr.getById(key);
				value = StaticMethod.null2String(sysUser.getUserName());
				SYS_USER.put(key, value);
			}
			return value;
		} else {
			return "";
		}
	}

}
